package com.open.capacity.reimburse.controller;

import java.util.Map;

import com.open.capacity.commons.Result;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.SysInvoice;
import com.open.capacity.reimburse.service.SysInvoiceService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 发票
 *
 * @author
 * @email
 * @date 2021-03-05 07:24:19
 */
@RestController
@RequestMapping("invoices")
@Api(tags = "发票")
public class SysInvoiceController {

    @Autowired
    private SysInvoiceService sysInvoiceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('sys_invoice:sysinvoice:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = sysInvoiceService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('sys_invoice:sysinvoice:list')")
    public Result findById(String id) {
        return Result.succeed(sysInvoiceService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('sys_invoice:sysinvoice:save')")
    public Result save(@RequestBody @Valid SysInvoice sysInvoice, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            sysInvoiceService.save(sysInvoice);
        } else {
            sysInvoice.setId(keyValue);
            sysInvoiceService.update(sysInvoice);
        }
        return Result.succeed("保存成功");
    }

    /**
     * 检查是否重复报销
     */
    @RequestMapping("/duplicateCheck")
    @PreAuthorize("hasAnyAuthority('sys_invoice:sysinvoice:duplicateCheck')")
    public Result duplicateCheck() {
        sysInvoiceService.duplicateCheck();
        return Result.succeed("操作成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('sys_invoice:sysinvoice:delete')")
    public Result delete(@PathVariable String id) {
        sysInvoiceService.delete(id);
        return Result.succeed("删除成功");
    }

}
